//引入http模块
const http = require('http')
//引入querystring
const querystring = require('querystring');
//引入router模块
const getRouter = require('router');
//引入模板引擎
const template = require('art-template');
//引入path模块
const path = require('path');
//引入静态资源访问模块
const serveStatic = require('serve-static');
//获取路由对象
const router = getRouter();

// 实现静态资源访问服务
const serve = serveStatic(path.join(__dirname, 'public'));

//配置模板的根目录
template.defaults.root = path.join(__dirname, 'views');

//呈递学生档案信息页面
router.get('/add', (req, res) => {
    let html = template('index.art', {});
    res.end(html);
})
//呈递学生档案信息列表页面
router.get('/list', async (req, res) => {
    let students = await Student.find();
    let html = template('list.art', {
        students: students
    })
    res.end(html);
})
//实现学生信息添加功能路由
router.post('/add', (req, res) => {
    let formData = '';
    req.on('data', param => {
        formData += param;
    });
    req.on('end', async () => {
        await Student.create(querystring.parse(formData));
        res.writeHead(301, {
            Location: '/list'
        });
        res.end();
    })
})

//引入数据库连接模块
require('./model/connect');
//引入学生信息集合
const Student = require('./model/user');
//连接服务器
const app = http.createServer();
//当客户端访问服务器的时候
app.on('request', (req, res) => {
    router(req, res, () => { });
    serve(req, res, () => { });
});

app.listen(80);
console.log('服务器启动成功');